<?php
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    //public $imageFile;

    public $imageFiles;


    /*public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg'],
        ];
    }*/

    //注：经行多图上传的时候，maxFiles如果设置为0则表示没有限制，但是在php.ini中默认max_file_uploads = 20
    public function rules()
    {
        return [
            [['imageFiles'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png,jpg', 'maxFiles' => 4],
        ];
    }

    /*public function upload()
    {
        if ($this->validate()) {
            //dd($this->imageFile->baseName);
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }*/

    /*文件可以同时提交到后台，但是需要save the uploaded files one by one*/
    public function upload()
    {
        if ($this->validate()) {
            //dd($this->imageFiles);
            foreach ($this->imageFiles as $file) {
                $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
            }
            return true;
        } else {
            return false;
        }
    }



    public function attributeLabels()
    {
        return [
            //'imageFile' => '图片上传',
            'imageFiles' => '多图片上传',
        ];
    }

}